package com.qianfeng.xinyihezi.xinyihezi.activity.activity_meng;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Toast;

import com.qianfeng.xinyihezi.xinyihezi.R;
import com.qianfeng.xinyihezi.xinyihezi.model.bean_meng.MySelf;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.tencent.qq.QQ;
import retrofit2.Callback;

public class LoginActivity extends AppCompatActivity implements PlatformActionListener {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_login);
        Toast.makeText(this, "请先登录", Toast.LENGTH_LONG).show();
        ShareSDK.initSDK(this);
    }

    public void click(View view) {
        switch (view.getId()) {
            case R.id.iv_login_done:
                finish();
                break;
            case R.id.tv_denglu:
                Toast.makeText(this, "登陆界面", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_zhuce:
                Toast.makeText(this, "注册界面", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_qq:
                login();
                break;
            case R.id.btn_weibo:
                Toast.makeText(this, "微博界面", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_wetchat:
                Toast.makeText(this, "微信界面", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
    }
    private void login() {
        Platform weibo = ShareSDK.getPlatform(QQ.NAME);
        weibo.SSOSetting(false);  //设置false表示使用SSO授权方式
        weibo.setPlatformActionListener(this); // 设置分享事件回调
        weibo.authorize();//单独授权
        weibo.showUser(null);//授权并获取用户信息
    }
    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        //授权完成
        if (!TextUtils.isEmpty(platform.getDb().getUserName())) {
            //拿到用户的信息之后一般存在两种选择
            //1.。让用户直接登录自己的服务器
            //2.。不让用户登陆，再次引导用户去注册界面
            Log.e("123", platform.getDb().getUserName());
            Log.e("123", platform.getDb().getUserIcon());
            Log.e("123", platform.getDb().getPlatformNname());
            MySelf mySelf = new MySelf();
            mySelf.name = platform.getDb().getUserName();
            mySelf.icon = platform.getDb().getUserIcon();
            EventBus.getDefault().post(mySelf);
            finish();
        }
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        //授权出错

    }

    @Override
    public void onCancel(Platform platform, int i) {
        //授权取消

    }

}
